﻿using EasyOAuth.Core.OAuth;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyOAuth.Service
{

    /// <summary>
    /// 用户session
    /// </summary>
    public interface IUserSession
    {

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        Dictionary<string, string> Get();

        /// <summary>
        /// 获取用户ID
        /// </summary>
        /// <returns></returns>
        int UserId { get; }

        /// <summary>
        /// 富文本框ID
        /// </summary>
        int EditorFileDirectoryId { get; }
    }

    /// <summary>
    /// 获取用户会话信息
    /// </summary>
    public class UserSessionContext : IUserSession
    {
        /// <summary>
        /// 
        /// </summary>
        IHttpContextAccessor _accessor;

        /// <summary>
        /// 获取用户会话信息
        /// </summary>
        /// <param name="accessor"></param>
        public UserSessionContext(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }


        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> Get()
        {
            var request = _accessor?.HttpContext.Request;
            var token = request?.Headers["Authorization"] ?? "";
            if (string.IsNullOrWhiteSpace(token))
            {
                return new();
            }
            //获取用户ID
            var accessToken = new AccessTokenBuilder();
            return accessToken.GetStoreData(token);
        }

        /// <summary>
        /// 登录ID
        /// </summary>
        public int UserId
        {
            get
            {
                return Convert.ToInt32(Get()["ID"]);
            }
        }

        /// <summary>
        /// 富文本框ID
        /// </summary>
        public int EditorFileDirectoryId
        {
            get
            {
                return Convert.ToInt32(Get()["EFDID"]);
            }
        }
    }

    /// <summary>
    /// JWT
    /// </summary>
    public class UserJwtSessionContext : IUserSession
    {
        /// <summary>
        /// 
        /// </summary>
        IHttpContextAccessor _accessor;

        /// <summary>
        /// 获取用户会话信息
        /// </summary>
        /// <param name="accessor"></param>
        public UserJwtSessionContext(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }


        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> Get()
        {
            var user = _accessor?.HttpContext.User;

            return new Dictionary<string, string>()
            {
                { "ID", user?.FindFirst("ID")?.Value ?? "0"},
                { "EFDID", user?.FindFirst("EFDID")?.Value ?? "0"}
            };
        }

        /// <summary>
        /// 登录ID
        /// </summary>
        public int UserId
        {
            get
            {
                return Convert.ToInt32(Get()["ID"]);
            }
        }

        /// <summary>
        /// 富文本框ID
        /// </summary>
        public int EditorFileDirectoryId
        {
            get
            {
                return Convert.ToInt32(Get()["EFDID"]);
            }
        }
    }
}
